<?php

class Front_Form_Editprofile extends Zend_Form
{


    public function init() {
        parent::init();
        
        $this->setMethod('post');
        $this->setName('editprofile');
        $this->setAction('/front/index/editprofile');
        
        $id = new Zend_Form_Element_Hidden('id');

        $username = new Zend_Form_Element_Text('username');  
        $username->setLabel('Username:')  
                ->setAttribs(array('placeholder' => 'username', 'readonly'=>'readonly'))
                ->setRequired(true)
                ;
      
        $password = new Zend_Form_Element_Password('new_password');  
        $password->setLabel('New Password:');
      
        $confirm = new Zend_Form_Element_Password('confirm');  
        $confirm->setLabel('Confirm Password:') 
                ->addValidator('Identical', false, array('token' => 'new_password'))
                ->addErrorMessage('The passwords do not match');

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name:')
                ->setAttrib('placeholder', 'name')
                ->setRequired(true)
                ->addValidator('StringLength', false, array(0, 255));
      
        $mobile = new Zend_Form_Element_Text('mobile');
        $mobile->setLabel('MobilePhone:')
                ->setAttrib('placeholder', 'mobile')
                ->setRequired(false);

        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email:')
                ->setAttrib('placeholder', 'email')
                ->setRequired(true)
                ->addValidator('EmailAddress')
                ;

        $department = new Zend_Form_Element_Select('department');
        $department->setLabel('Department:')
                ->setRequired(false)
                ;
        
        $_em = Zend_Registry::get('entitymanager');
        $depts = $_em->getRepository('Entities\Department')->findAllList();
        $department->addMultiOptions($depts);
        
        $submit = new Zend_Form_Element_Submit('Save');  
        $submit->setLabel('Save')
                ->setAttribs(array('class'=>'btn btn-primary'))
                ;  
      
        $reset = new Zend_Form_Element_Reset('Reset');  
        $reset->setLabel('Reset')
                ->setAttribs(array('class'=>'btn'))
                ;

        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form')),
            array('Description', array('placement' => 'prepend')),
            'Form'
        ));
      
        $this->addElement($id)
                ->addElement($username)  
                ->addElement($password)  
                ->addElement($confirm)
                ->addElement($name)
                ->addElement($mobile)
                ->addElement($email)
                ->addElement($department)
                ->addElement($submit)
                ->addElement($reset);  
    }
    

    public function isValid($data) {
        
        $emailUnique = new Front_Validator_NoRecordExists(
            array('entityManager' => Zend_Registry::get('entitymanager'),
                'class' => 'Entities\User',
                'property' => 'email',
                'exclude' => array(
                array('property' => 'id', 'value' => $this->getValue('id'))
                )
            )
        );
        
        $this->getElement('email')->addValidator($emailUnique);
        
        return parent::isValid($data);
    }
}
